/****************************************************************
 * <strong>MFSqlUpdate基类</strong>
 *
 * <p>
 * MFSqlUpdate基类
 * </p>
 *
 * @author AndyZhang 2011 cnet2001@163.com
 * @version 0.1
 * ToDo:
 ***************************************************************/

package com.thinkwide.data.ormapping.sql;

import com.thinkwide.data.core.MFString;

import java.util.List;

public class MFSqlUpdate extends MFSqlBase {
    public static final String UPDATE = "Update %s SET %s %s ";

    public static String getUpdateSql(String tableName, String values, String where) {

        String strSQL = String.format(UPDATE, tableName, values, WHERE + where);
        return strSQL;

    }

    public static String getUpdateSqlParam(String tableName, List<String> fields, List<String> primaryKeys) {
        String strFields = MFString.Empty;
        for (String item : fields) {
            if (MFString.notNullOrEmpty(strFields)) {
                strFields = strFields + COMMA + item + EQUAL + PARAM;

            } else {
                strFields = item + EQUAL + PARAM;

            }

        }

        String where = MFString.Empty;

        for (String item : primaryKeys) {
            if (MFString.notNullOrEmpty(where)) {
                where = where + AND;
            }
            where = where + item + "= ? ";
        }

        String sSQL = MFSqlUpdate.getUpdateSql(tableName, strFields, where);

        return sSQL;
    }
}
